class MobsController < ApplicationController
  before_filter :require_officer, :except => [ :show ]

  def show
    if is_officer?
      redirect_to :action => :edit
    else
      flash[:notice] = "Showing detailed info about mobs hasn't been implemented yet."
      redirect_to :back
    end
  end

  def create
    mob = Mob.new(params[:mob])
    if mob.save
      loot_table = LootTable.new(:mob => mob)
      loot_table.save
      flash[:notice] = "#{mob.name} added to #{mob.zone.name}."
      redirect_to :back
    end
  end

  def edit
    @mob = Mob.find(params[:id])
    @page_description = @mob.name
    @items = Item.find(:all)
    @mob.loot_table.items.each { |i|
      if @items.include?(i)
        @items.delete(i)
      end
    }
  end

  def add_item_to_loot_table
    @mob = Mob.find(params[:id])
    item = Item.find(params[:item][:item_id])
    loot_table_item = LootTableItemList.new(:loot_table => @mob.loot_table, :item => item)
    if loot_table_item.save
      flash[:notice] = "#{item.name} added to loot table."
    else
      if @mob.loot_table.items.include?(loot_table_item.item)
        flash[:notice] = "#{item.name} is already in the loot table."
      else
        flash[:notice] = "#{item.name} couldn't be saved."
      end
    end
    redirect_to :action => :edit, :id => @mob.id
  end

  def update
    if request.get?
      flash[:notice] = "Click the mob you wish to edit."
    else
      mob = Mob.find params[:id]
      if mob.update_attributes(params[:mob])
        flash[:notice] = "#{mob.name} updated."
      else
        flash[:notice] = "Failed to update #{mob.name}."
      end
    end
    redirect_to :controller => "zones", :action => :index
  end

end
